<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class mail extends CI_Controller{
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION["isadmin"]) || !$_SESSION["isadmin"]){
			header("location:/index.php/login");
		}
	}
	/*邮件列表*/
	public function mailList(){
            $url = "/index.php?c=mail&a=mailList";
            $uid = isset( $_GET['uid']) && 0 < intval($_GET['uid']) ? intval($_GET['uid']) : 0;
            $data['uid'] = $uid;
            $where = "";
            if( $uid ){
                $where  = " WHERE ( m.sendUserID = '{$uid}' or m.AcceptUserID = '{$uid}')" ;
                $url.= "&uid=".$uid;
            }
            
            $userID = isset( $_GET['userID']) && 0 < intval($_GET['userID']) ? intval($_GET['userID']) : '';
            $data['userID'] = $userID;
            if( $userID ){
                $where  = " WHERE ( m.sendUserID = '{$userID}' or m.AcceptUserID = '{$userID}')" ;
                $url.= "&userID=".$userID;
            }
            
            $this->config->load('pagination',true);
            $pagesize  = $this->config->item("per_page","pagination");
            $sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
            $where .= " LIMIT $sposition ,$pagesize ";
            $sql = " SELECT m.* FROM pk_mail as m $where ";
            $this->load->model('MailModel');
            $data['mailList'] = $this->MailModel->getAll($sql);
            
            $this->load->library('pagination');
            $config['base_url'] = $url;
            $config['total_rows'] = $this->MailModel->mailCount($where);
            $this->pagination->initialize($config);
            $data['page'] = $this->pagination->create_links();
            
            
            $this->load->view("header");
            $this->load->view("mail/mailList",$data);
            $this->load->view("footer");
	}
        
        public function mailAdd(){
            $this->load->model('MailModel');
            $sql = " SELECT u.id,ui.userName FROM pk_user AS u LEFT JOIN pk_user_info AS ui ON u.id = ui.userID WHERE u.state = 0 ";
            $data['sendInfo'] = $this->MailModel->getAll($sql);
            $this->load->view("header");
            $this->load->view("mail/mailAdd",$data);
            $this->load->view("footer");
        }
        
        public function sendMail(){
            $set = array();
            if(!empty($_GET['title'])){
                $set[] = "title ='".$_GET['title']."'";
            }
            if(!empty($_GET['sendInfo'])){
                $sendInfo = explode('-', $_GET['sendInfo']);
                $set[] = "sendUserID ='".$sendInfo[0]."'";
                $set[] = "sendUserName ='".$sendInfo[1]."'";
            }
            if(!empty($_GET['acceptInfo'])){
                $accepInfo = explode('-', $_GET['acceptInfo']);
                $set[] = "AcceptUserID ='".$accepInfo[0]."'";
                $set[] = "AcceptUserName ='".$accepInfo[1]."'";
            }
            
            if(!empty($_GET['descripe'])){
                $set[] = "descripe ='".$_GET['descripe']."'";
            }
            if(!empty($_GET['jetton'])){
                $set[] = "jetton ='".$_GET['jetton']."'";
            }
            
            if( 7 != count($set) ){
                mcmessage_error("你有内容未输入", '.index.php?c=mail&a=mailAdd');
                exit;
            }
            $set[] = "mailType = '1'";
            $set[] = "ctime='".time()."'";
            $set[] = "isUser='1'";
            
            $this->load->model('MailModel');
            
            $sql = "INSERT INTO pk_mail SET ".implode(',', $set);
            if( $this->MailModel->sqlquery($sql) ){
                mcmessage_error("发送邮件成功", '/index.php?c=mail&a=mailList');
            }else{
                mcmessage_error("发送邮件失败", '/index.php?c=mail&a=mailList');
            }
        }
        
        public function getUserFriend(){
            $uid = $_GET['userID'];
            $this->load->model('MailModel');
            $sql = " SELECT * FROM pk_user_friend  WHERE userID = '{$uid}' AND state = '2' ";
            $user = $this->MailModel->getAll($sql);
            echo json_encode($user);
        }
                 
}